import { Component, EventTouch, Input, Node, UITransform, Vec2, Vec3, _decorator } from 'cc';
import ctrlMgr from '../../frame/input/InputCtrl';
const { ccclass, property } = _decorator;

const temp_v3 = new Vec3();

@ccclass('Joystick')
export class Joystick extends Component {

    @property(Node)
    round!: Node;//摇杆背景

    @property(Node)
    inner!: Node;//摇杆 也就是中心点

    @property
    isStatic: boolean = true; //固定罗盘不隐藏

    @property
    isDiretion: boolean = false; //是否为方向模式(中心指示点拉尽)

    @property
    maxRadius: number = 128;

    @property
    activeRange: number = 0.1; //摇杆触发范围比例（0-1）

    public touchID: number | null = -1;

    private data = { ratio: 0, angle: 0 };


    onEnable() {

        this.show(this.isStatic);
        if (this.isStatic) {
            this.round.on(Node.EventType.TOUCH_START, this.touchStart, this);
            this.round.on(Node.EventType.TOUCH_MOVE, this.touchMove, this);
            this.round.on(Node.EventType.TOUCH_END, this.touchEnd, this);
            this.round.on(Node.EventType.TOUCH_CANCEL, this.touchEnd, this);
        }

    }
    protected onDisable(): void {
        if (this.isStatic) {
            this.round.off(Node.EventType.TOUCH_START, this.touchStart, this);
            this.round.off(Node.EventType.TOUCH_MOVE, this.touchMove, this);
            this.round.off(Node.EventType.TOUCH_END, this.touchEnd, this);
            this.round.off(Node.EventType.TOUCH_CANCEL, this.touchEnd, this);
        }
        this.inner.position = Vec3.ZERO;
        ctrlMgr.v = ctrlMgr.h = 0;
        ctrlMgr.ratio = 1;
    }

    show(flag: boolean) {
        this.round.active = flag;
    }


    innerPosition(pos: Vec2) {


        let ui = this.round.getComponent(UITransform);
        let s = ui!.convertToNodeSpaceAR(temp_v3.set(pos.x, pos.y));
        s.z = 0;

        const length = s.length();

        //触发范围
        if (length <= this.maxRadius * this.activeRange) {
            this.inner.position = Vec3.ZERO;
            ctrlMgr.rad = 0;
            ctrlMgr.ratio = 0;
        }

        //限制范围
        if (length > this.maxRadius || this.isDiretion) {
            s = s.normalize();
            s = s.multiplyScalar(this.maxRadius);
        }
        this.inner.setPosition(s); //修正位置
        ctrlMgr.h = s.y;
        ctrlMgr.v = s.x;

        //实际数据
        const ratio = Math.max(0.1, s.length() / this.maxRadius);
        ctrlMgr.ratio = ratio;


    }


    touchStart(event: EventTouch) {

        if (this.touchID == -1) {
            this.touchID = event.getID();

            if (!this.isStatic) {
                this.show(true);
                let pos = event.getUILocation();
                this.node.setWorldPosition(pos.x, pos.y, 0);
            }
        }


        if (this.touchID != event.getID()) return false;
        this.innerPosition(event.getUILocation());

    }

    touchMove(event: EventTouch) {
        if (this.touchID != event.getID()) return false;
        this.innerPosition(event.getUILocation());

    }



    touchEnd(event: EventTouch) {//摇杆弹回原位置
        if (this.touchID != event.getID()) return false;

        this.touchID = -1;
        this.show(this.isStatic);

        this.inner.position = Vec3.ZERO;
        ctrlMgr.v = ctrlMgr.h = 0;

        ctrlMgr.ratio = 1;

    }

}